-- markdown 预览完整配置
local function setup_markdown_preview()
  -- markdown-preview.nvim
  vim.g.mkdp_auto_start = 0
  vim.g.mkdp_auto_close = 1
  vim.g.mkdp_refresh_slow = 0
  vim.g.mkdp_command_for_global = 0
  vim.g.mkdp_open_to_the_world = 0
  vim.g.mkdp_open_ip = ""
  vim.g.mkdp_browser = ""
  vim.g.mkdp_echo_preview_url = 0
  vim.g.mkdp_page_title = "「${name}」"
  vim.g.mkdp_filetypes = { "markdown" }

  -- 数学公式配置
  vim.g.mkdp_math = 1
  vim.g.mkdp_preview_options = {
    mkit = {},
    katex = {
      js_path = "https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.8/katex.min.js",
      css_path = "https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.8/katex.min.css",
    },
    mathjax = {
      js_path = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js",
    },
    disable_sync_scroll = 0,
    sync_scroll_type = "middle",
    hide_yaml_meta = 1,
    sequence_diagrams = {},
    flowchart_diagrams = {},
    content_editable = false,
    disable_filename = 0,
    toc = {},
  }

  -- 快捷键
  vim.keymap.set("n", "<leader>mp", ":MarkdownPreview<CR>", { desc = "Markdown Preview" })
  vim.keymap.set("n", "<leader>ms", ":MarkdownPreviewStop<CR>", { desc = "Markdown Preview Stop" })
end

return {
  -- 使用 lazy.nvim
  {
    "iamcco/markdown-preview.nvim",
    build = function()
      vim.fn["mkdp#util#install"]()
    end,
    ft = "markdown",
    init = setup_markdown_preview,
    config = function()
      vim.g.mkdp_theme = "dark"
      vim.g.mkdp_math = 1 -- 启用数学公式支持
      vim.g.mkdp_preview_options = {
        mkit = {},
        katex = {
          js_path = "https://cdn.jsdelivr.net/npm/katex@0.16.8/dist/katex.min.js",
          css_path = "https://cdn.jsdelivr.net/npm/katex@0.16.8/dist/katex.min.css",
        },
        mathjax = {
          js_path = "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js",
        },
      }
    end,
  },
  -- 使用 marksman + 浏览器预览
  -- {
  --   -- 安装 marksman LSP：
  --   -- mason 配置
  --   require("mason").setup()
  --   require("mason-lspconfig").setup({
  --     ensure_installed = { "marksman" }
  --   })
  -- },
}
